package com.github.niefy.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.niefy.modules.wx.entity.ChangduUserInfo;
import com.github.niefy.modules.wx.entity.ChangduUserRecharge;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ChangDuCallService {
    @Autowired
    private ChangDuCall changDuCall;

    public List<ChangduUserInfo> getUserListV1() {
        String userListV1 = changDuCall.getUserListV1();
        if (StringUtils.isBlank(userListV1)) {
            return Lists.newArrayList();
        }
        JSONObject jsonObject = JSONObject.parseObject(userListV1);
        if (!jsonObject.containsKey("data")) {
            return Lists.newArrayList();
        }
        JSONArray dataList = jsonObject.getJSONArray("data");
        return dataList.toJavaList(ChangduUserInfo.class);
    }

    public Map<String, ChangduUserInfo> getUserListV1ReturnOpenIdMap() {
        List<ChangduUserInfo> userList = getUserListV1();
        if (ObjectUtils.isEmpty(userList)) {
            return Maps.newHashMap();
        }
        return userList.stream().collect(Collectors.toMap(ChangduUserInfo::getOpenId, c -> c, (key1, key2) -> key1));
    }

    /**
     * 获取一整天的所有数据
     */
    public List<ChangduUserRecharge> getUserReChargeV1() {
        List<ChangduUserRecharge> resultList = new ArrayList<>(1000);

        // LocalDateTime endTime = LocalDateTime.now();
        // LocalDateTime startTime = endTime.plusHours(-24);
        // LocalDateTime startTime = endTime.plusDays(-90);

        int count = 0;
        do {
            if (count > 10000) {
                break;
            }

            try {
                String userReChargeV1 = changDuCall.getUserReChargeV1(count);
                if (StringUtils.isBlank(userReChargeV1)) {
                    break;
                }
                JSONObject jsonObject = JSON.parseObject(userReChargeV1);
                if (!jsonObject.containsKey("result")) {
                    break;
                }
                JSONArray resultJson = jsonObject.getJSONArray("result");
                if (ObjectUtils.isEmpty(resultJson)) {
                    break;
                }
                List<ChangduUserRecharge> tempRechargeList = resultJson.toJavaList(ChangduUserRecharge.class);
                resultList.addAll(tempRechargeList);
            } catch (Exception e) {
                log.error("ChangDuCallService getUserReChargeV1 error {}", e.getMessage(), e);
            } finally {
                count = resultList.size();
            }
        } while (true);

        // 使用 Stream API 进行去重
        return new ArrayList<>(resultList.stream()
                .collect(Collectors.toMap(ChangduUserRecharge::getTradeNo, p -> p, (existing, replacement) -> existing))
                .values());
    }

}
